package com.maewa.links;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

public class LinkExtractor {
	public static void main(String[] args) throws Exception {
		for (String link : args) extractLinks(link);
	}

	private static void extractLinks(String link) throws MalformedURLException,
			IOException {
		URL url = new URL(link);
		InputStream is = url.openStream();
		String html = IOUtils.toString(is);
		
		int startIndex = 0, endIndex = 0;
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		
		while(true) {
			startIndex = html.indexOf("href=\"", startIndex);
			if(startIndex == -1) break;
			
			endIndex = html.indexOf('"', startIndex + 6);
			
			String urlString = html.substring(startIndex + 6, endIndex);
			if(urlString.contains(".html")) {
				int folderStart = html.indexOf("<span class=\"head\">", endIndex) + 19;
				int folderEnd = html.indexOf("</span>", folderStart);
				String folderName = html.substring(folderStart, folderEnd);
				
				if(folderName.contains("(")) {
					String year = StringUtils.trim(StringUtils.substringBetween(folderName, "(", ")"));
					folderName = StringUtils.trim(StringUtils.substringBefore(folderName, "("));
					
					if(map.containsKey(year)) {
						map.get(year).add(urlString + " " + folderName);
					} else {
						List<String> urls = new ArrayList<String>();
						urls.add(urlString + " " + folderName);
						map.put(year, urls);
					}
				} else {
					String year = "unknown";
					folderName = StringUtils.trim(StringUtils.substringBefore(folderName, "("));
					
					if(map.containsKey(year)) {
						map.get(year).add(urlString + " " + folderName);
					} else {
						List<String> urls = new ArrayList<String>();
						urls.add(urlString + " " + folderName);
						map.put(year, urls);
					}
				}
			}

			startIndex = endIndex + 1;
		}
		
		for(String key : map.keySet()) {
			File outFile = new File("e:/" + key + ".txt");
			FileUtils.writeLines(outFile, map.get(key), true);
		}
	}
}
